package com.minelx.datamicroscope.querylanguage.data.sql;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.minelx.datamicroscope.querylanguage.data.BinaryOperatorCondition;

import java.util.Objects;

public class BinaryOperatorProvider implements ConditionProvider {

	private final SQLExpr left;

	private final String operator;

	private final SQLExpr right;

	public BinaryOperatorProvider(SQLExpr left, String operator, SQLExpr right) {
		this.left = left;
		this.right = right;
		this.operator = operator;
	}

	@Override
	public String literalKind() {
		return operator;
	}

	@Override
	public FieldBelongs left() {
		return FieldBelongs.from(left.toString());
	}

	@Override
	public String right() {
		return right.toString();
	}

	@Override
	public BinaryOperatorCondition toCondition() {
		return new BinaryOperatorCondition(left().fieldName(), operator, right());
	}

	@Override
	public String toString() {
		return "BinaryOperatorProvider{" +
				"left=" + left +
				", operator='" + operator + '\'' +
				", right=" + right +
				'}';
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		BinaryOperatorProvider that = (BinaryOperatorProvider) o;
		return left.equals(that.left) && operator.equals(that.operator) && right.equals(that.right);
	}

	@Override
	public int hashCode() {
		return Objects.hash(left, operator, right);
	}
}
